#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <qDebug>
#include <QByteArray>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //获取系统所有可用的串口
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        qDebug()<<info.portName()<<info.description();
        QSerialPort serial(info);

        if(serial.open(QIODevice::ReadWrite)) //空闲
        {
            //将串口名加入选项列表
            ui->comboBox_serialport->addItem(serial.portName());
            //关闭串口
            serial.close();
        }
        else //被占用
        {
            ui->comboBox_serialport->addItem(serial.portName()+"(被占用)");
        }
    }

    ui->pushButton_SendMsg->setEnabled(false);
    ui->comboBox_baudrate->setCurrentIndex(4);
    ui->comboBox_databits->setCurrentIndex(3) ;

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_SendMsg_clicked()
{
    qDebug()<<"开始发送数据";
    QByteArray data = ui->textEdit->toPlainText().toUtf8();
    my_serial->write(QByteArray::fromHex(data));
    qDebug()<<data;
}
//接收数据
void MainWindow::readData()
{
    //接收所有数据
    QByteArray data = my_serial->readAll();
    qDebug()<<"接收的数据："<<data;
    if(!data.isEmpty())
    {
        ui->textBrowser->append(QString(data.toHex()));
    }
}

void MainWindow::on_pushButton_openclose_clicked()
{
    if(ui->pushButton_openclose->text()=="打开串口")
    {
        my_serial = new QSerialPort(ui->comboBox_serialport->currentText(),this);
        //打开串口
        if(!my_serial->open(QIODevice::ReadWrite))
        {
            qDebug()<<"打开失败";
            my_serial->deleteLater();
            return;
        }

        //设置波特率
        switch (ui->comboBox_baudrate->currentIndex())
        {
        case 0:
            my_serial->setBaudRate(QSerialPort::Baud2400);
            break;
        case 1:
            my_serial->setBaudRate(QSerialPort::Baud4800);
            break;
        case 2:
            my_serial->setBaudRate(QSerialPort::Baud9600);
            break;
        case 3:
            my_serial->setBaudRate(QSerialPort::Baud38400);
            break;
        case 4:
            my_serial->setBaudRate(QSerialPort::Baud115200);
            break;
        }

        //设置数据位位数
        switch (ui->comboBox_databits->currentIndex()) {
        case 0:
            my_serial->setDataBits(QSerialPort::Data5);
            break;
        case 1:
            my_serial->setDataBits(QSerialPort::Data6);
            break;
        case 2:
            my_serial->setDataBits(QSerialPort::Data7);
            break;
        case 3:
            my_serial->setDataBits(QSerialPort::Data8);
            break;
        }

        //设置校验位
        switch (ui->comboBox_parity->currentIndex()) {
        case 0:
            my_serial->setParity(QSerialPort::NoParity);
            break;
        case 1:
            my_serial->setParity(QSerialPort::OddParity);
            break;
        case 2:
            my_serial->setParity(QSerialPort::EvenParity);
            break;
        }

        //设置停止位
        switch (ui->comboBox_parity->currentIndex()) {
        case 0:
            my_serial->setStopBits(QSerialPort::OneStop);
            break;
        case 1:
            my_serial->setStopBits(QSerialPort::TwoStop);
            break;
        }

        //关闭流控
        my_serial->setFlowControl(QSerialPort::NoFlowControl);

        //关闭选项菜单
        ui->comboBox_parity->setEnabled(false);
        ui->comboBox_baudrate->setEnabled(false);
        ui->comboBox_databits->setEnabled(false);
        ui->comboBox_stopbits->setEnabled(false);
        ui->comboBox_serialport->setEnabled(false);
        ui->pushButton_SendMsg->setEnabled(true);

        //修改按钮显示
        ui->pushButton_openclose->setText("关闭串口");

        //连接信号和槽
        QObject::connect(my_serial,&QSerialPort::readyRead,this,&MainWindow::readData);
    }
    else //关闭
    {
        my_serial->clear();
        my_serial->close();
        my_serial->deleteLater();

        ui->comboBox_parity->setEnabled(true);
        ui->comboBox_baudrate->setEnabled(true);
        ui->comboBox_databits->setEnabled(true);
        ui->comboBox_stopbits->setEnabled(true);
        ui->comboBox_serialport->setEnabled(true);
        ui->pushButton_SendMsg->setEnabled(false);

        ui->pushButton_openclose->setText("打开串口");

    }
}


void MainWindow::on_pushButton_clear_recv_text_clicked()
{
    ui->textBrowser->clear();
}


void MainWindow::on_pushButton_clear_send_text_clicked()
{
    ui->textEdit->clear();
}


